package org.geometerplus.fbreader.library;

import java.lang.ref.SoftReference;

import org.geometerplus.android.fbreader.RLog;
import org.zixuanstudio.ebook.R;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;

public class BooksCursorAdapter extends CursorAdapter {

	private static final String TAG = "BookCursorAdapter";

	private static final int BOOK_ID = 0;
	private static final int ENCODING = 1;
	private static final int LANGUATE = 2;
	private static final int TITLE = 3;
	private static final int BOOK_COVER = 4;
	private static final int PATH = 5;
	private static final int FILE_ID = 6;

	private Context mContext;
	private Cursor mCursor;

	private BookCoverCache mCache = null;
	private BitmapDrawable mDefaultDrawable = null;

	public BooksCursorAdapter(Context context, Cursor c) {
		super(context, c);
		mContext = context;
		mCursor = c;
		mCache = BookCoverCache.getInstance();
		mDefaultDrawable = new BitmapDrawable(BitmapFactory.decodeResource(
				context.getResources(), R.drawable.default_cover));
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ImageView bookCover = (ImageView) view.findViewById(R.id.book_cover);
		//TODO:统计加入Cache后是否在性能上有所提升
		Drawable drawable = mCache.get(cursor);
		if (drawable != null) {
			bookCover.setBackgroundDrawable(drawable);
		} else {
			bookCover.setBackgroundDrawable(mDefaultDrawable);
		}
		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return LayoutInflater.from(context).inflate(R.layout.shelf_book, null);
	}

	@Override
	protected void onContentChanged() {
		// TODO Auto-generated method stub
		super.onContentChanged();
	}

	/*
	 * @Override public void notifyDataSetChanged() {
	 * //super.notifyDataSetChanged();
	 * RLog.v("BooksCursorAdapter","notifyDataSetchanged"); mCursor.requery();
	 * 
	 * }
	 */

}
